/*
 * Copyright @ 2021 bingye
 * com.iflytek.zhcs.msjx.portal.common 19:08
 * All right reserved.
 */

package com.example.demo.utils;

import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * @desc:通用返回结果
 * @author: weili23
 * @params
 * @Date: 2024/5/8 9:38
 * @version: 1.0
 */
public class CommonResult<T> implements Serializable {

    public interface ResultMsg {
        String SUCCESS ="成功";
        String FAILURE ="失败";
        String UPGRADE = "系统维护中";
    }

    /**
     * 实体对象
     */
    @ApiModelProperty(value = "实体对象")
    private T data;

    /**
     * 操作标志 true 真，false 假
     */
    @ApiModelProperty(value = "操作标志 true 真，false 假")
    private Boolean flag;

    /**
     * 响应编码 200 成功，404 未找到，403 禁止，500 异常 （其他自定义10xx）
     */
    @ApiModelProperty(value = "响应编码 200 成功，404 未找到，403 禁止，500 异常 （其他自定义10xx）")
    private Integer code;

    /**
     * 提示消息
     */
    @ApiModelProperty(value = "提示消息")
    private String message;

    /**
     * 成功
     */
    public static <T> CommonResult<T> success(T data) {
        return new CommonResult<T>(data, true, 200, "成功");
    }

    public static <T> CommonResult<T> success(T data, String message) {
        return new CommonResult<T>(data, true, 200, message);
    }
    /**
     * 失败
     */
    public static <T> CommonResult<T> fail() {
        return new CommonResult<>(null, false, 404, "失败");
    }

    public static <T> CommonResult<T> fail(T data,String message) {
        return new CommonResult<>(data, false, 404, message);
    }

    public static <T> CommonResult<T> fail(String message) {
        return new CommonResult<>(null, false, 404, message);
    }

    public static <T> CommonResult<T> fail(String message, Integer code) {
        return new CommonResult<>(null, false, code, message);
    }

    /**
     * 禁止
     */
    public static <T> CommonResult<T> forbid() {
        return new CommonResult<>(null, false, 403, "无权限访问");
    }

    public static <T> CommonResult<T> forbid(String message) {
        return new CommonResult<>(null, false, 403, message);
    }

    /**
     * 异常
     */
    public static <T> CommonResult<T> error() {
        return new CommonResult<>(null, false, 400, "网络异常,请稍候重试!");
    }

    public static <T> CommonResult<T> error(String message) {
        return new CommonResult<>(null, false, 400, message);
    }

    public static <T> CommonResult<T> error(Integer code, String message) {
        return new CommonResult<>(null, false, code, message);
    }

    public CommonResult() {
    }

    public CommonResult(T data, Boolean flag, Integer code, String message) {
        this.data = data;
        this.flag = flag;
        this.code = code;
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Boolean getFlag() {
        return flag;
    }

    public void setFlag(Boolean flag) {
        this.flag = flag;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
